package com.family.web.controller.pedigree;


import com.family.common.annotation.Log;
import com.family.common.core.controller.BaseController;
import com.family.common.core.domain.AjaxResult;
import com.family.common.core.page.TableDataInfo;
import com.family.common.enums.BusinessType;
import com.family.common.utils.StringUtils;
import com.family.pedigree.domain.FamilyAlbumRecord;
import com.family.pedigree.service.IFamilyAlbumRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 生活明细表 前端控制器
 * </p>
 *
 * @author forever
 * @since 2024-11-25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/family/album/record")
public class FamilyAlbumRecordController extends BaseController {

    private final IFamilyAlbumRecordService familyAlbumRecordService;

    @Log(title = "添加生活明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FamilyAlbumRecord record) {
        familyAlbumRecordService.save(record.setCreateBy("admin").setCreateTime(LocalDateTime.now()));
        return success();
    }

    @Log(title = "修改生活明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FamilyAlbumRecord record) {
        familyAlbumRecordService.updateById(record);
        return success();
    }

    @GetMapping("/list")
    public TableDataInfo list(FamilyAlbumRecord record){
        startPage();
        return getDataTable(familyAlbumRecordService.lambdaQuery()
                .eq(FamilyAlbumRecord::getAlbumId,record.getAlbumId())
                .like(StringUtils.isNotBlank(record.getPhotoName()), FamilyAlbumRecord::getPhotoName, record.getPhotoName())
                .like(StringUtils.isNotBlank(record.getPhotoType()), FamilyAlbumRecord::getPhotoType, record.getPhotoType())
                .orderByDesc(FamilyAlbumRecord::getCreateTime).list());
    }

    @Log(title = "删除生活明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordId}")
    public AjaxResult delete(@PathVariable Long recordId) {
        familyAlbumRecordService.removeById(recordId);
        //todo:删除照片
        return success();
    }

}
